package com.zx.dsptuneup_TriMic.Util;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.zx.dsptuneup_TriMic.R;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * The type Common util.
 */
public class CommonUtil {
    /**
     * The constant spname1.
     */
    public static String spname1 = "自定义1";
    /**
     * The constant spname2.
     */
    public static String spname2 = "自定义2";
    /**
     * The constant spname3.
     */
    public static String spname3 = "自定义3";
    /**
     * The constant spname4.
     */
    public static String spname4 = "自定义4";

    /**
     * 保存EQ策略
     *
     * @param <K>    the type parameter
     * @param <V>    the type parameter
     * @param index  the index
     * @param eqArr  the eq arr
     * @param eqName the eq name
     */
    public static  <K, V> void saveEQtacti(Integer index,int[] eqArr,String eqName) {
        String SPNAME = null;
        switch (index) {
            case 0:
                SPNAME = spname1;
                break;
            case 1:
                SPNAME = spname2;
                break;
            case 2:
                SPNAME = spname3;
                break;
            case 3:
                SPNAME = spname4;
                break;
            default:
                break;
        }
        Map<String, int[]> map = new HashMap<>();
        map.put(eqName,eqArr);
        CommonUtil.putHashMapData(map,SPNAME);
    }

    /**
     * 用于保存集合
     *
     * @param <K>    the type parameter
     * @param <V>    the type parameter
     * @param map    map数据
     * @param SPNAME the spname
     * @return 保存结果 boolean
     */
    public static <K, V> boolean putHashMapData(Map<K, V> map, String SPNAME) {
        boolean result;
        try {
            Gson gson = new Gson();
            String json = gson.toJson(map);
            SharedPreferencesUtils.getInstance().putString(SPNAME,json);
            result = true;
        } catch (Exception e) {
            result = false;
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 用于取出集合
     *
     * @param <V>   the type parameter
     * @param index the index
     * @return HashMap hash map data
     */
    public static <V> HashMap<String, int[]> getHashMapData(Integer index) {
        String SPNAME = null;
        switch (index) {
            case 0:
                SPNAME = spname1;
                break;
            case 1:
                SPNAME = spname2;
                break;
            case 2:
                SPNAME = spname3;
                break;
            case 3:
                SPNAME = spname4;
                break;
            default:
                break;
        }

        Type type = new TypeToken<HashMap<String, int[]>>(){}.getType();
        String json = SharedPreferencesUtils.getInstance().getString(SPNAME);
        Gson gson = new Gson();
        JsonParser jsonParser = new JsonParser();
        HashMap<String, int[]> map = new HashMap<>();
        try {
            JsonObject obj= jsonParser.parse(json).getAsJsonObject();
            map = gson.fromJson(obj,type);

        } catch (Exception e) {  //  第一次进来没有值，给出默认值
            map.put(SPNAME,new int[]{4,0,1,2,3,4,5,4,3,3}) ;
            e.printStackTrace();
        }

        for (int[] val: map.values()) {
            Log.d("getHashMapData","map values===="+ Arrays.toString(val));
        }

        return map;
    }


    /**
     * 读取json文件输出字符串
     *
     * @param fileName the file name
     * @param context  the context
     * @return the json
     */
    public static String getJson(String fileName, Context context) {
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取assets资源管理器
            AssetManager assetManager = context.getAssets();
            //通过管理器打开文件并读取
            BufferedReader bf = new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }


    public static Bitmap changeBitmapSize(Bitmap bitmap,int newWidth,int newHeight) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Log.e("width","width:"+width);
        Log.e("height","height:"+height);

        //计算压缩的比率
        float scaleWidth=((float)newWidth)/width;
        float scaleHeight=((float)newHeight)/height;

        //获取想要缩放的matrix
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth,scaleHeight);

        //获取新的bitmap
        bitmap=Bitmap.createBitmap(bitmap,0,0,width,height,matrix,true);
        bitmap.getWidth();
        bitmap.getHeight();
        Log.e("newWidth","newWidth"+bitmap.getWidth());
        Log.e("newHeight","newHeight"+bitmap.getHeight());
        return bitmap;
    }
}
